package com.cjst.controller.api.search;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.dto.SearchDto;
import com.cjst.pojo.part.Parts;
import com.cjst.service.part.PartsService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.common.ResultCode;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping(value="/api/search")
public class SearchController {
	private Logger log=Logger.getLogger(SearchController.class);
	
	@Autowired
	private PartsService partsService;
	@Value("${file.path}")
	private String filePath;
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public String search(String keyword,String userId) {
		List<SearchDto> sdList=new ArrayList<SearchDto>();
		if(keyword==null||"".equals(keyword.trim())) {
			ResultObj robj = new ResultObj(ResultCode.RESULT_EXCEPTION, "关键字为空", null);
			return robj.toString();
		}
		try {
			if (keyword.indexOf("分析")!=-1 || keyword.indexOf("报告")!=-1 || keyword.indexOf("析")!=-1 || keyword.indexOf("决策")!=-1 || keyword.indexOf("辅助")!=-1 || keyword.indexOf("人数")!=-1 || keyword.indexOf("人流")!=-1) {
				SearchDto sd=new SearchDto();
				sd.setId("1111");
				sd.setName("辅助决策-龙华区区公园园内实时人数汇总表2019.11.27");
				sd.setType(0);
				sd.setDownUrl(filePath+"/work/fzjc1.docx");
				//sd.setImageUrl(filePath+"/work/fzjc.docx");
				sd.setAddress(null);
				sdList.add(sd);
				SearchDto sd2=new SearchDto();
				sd2.setId("2222");
				sd2.setName("辅助决策-深圳城市公园园内实时人数汇总表2019.11.27");
				sd2.setType(0); 
				sd2.setDownUrl(filePath+"/work/fzjc2.docx");
				//sd.setImageUrl(filePath+"/work/fzjc.docx");
				sd2.setAddress(null);
				sdList.add(sd2);
			}
			if(keyword.indexOf("龙华")!=-1 || keyword.indexOf("区公园")!=-1){
				int flag=0;
				if(sdList!=null && sdList.size()>0){
					for (SearchDto searchDto : sdList) {
						if(searchDto.getId().equals("1111")){
							flag=1;
						}
					}
				}
				if(flag==0){
					SearchDto sd=new SearchDto();
					sd.setId("1111");
					sd.setName("辅助决策-龙华区区公园园内实时人数汇总表2019.11.27");
					sd.setType(0);
					sd.setDownUrl(filePath+"/work/fzjc1.docx");
					//sd.setImageUrl(filePath+"/work/fzjc.docx");
					sd.setAddress(null);
					sdList.add(sd);
				}
			}
			if(keyword.indexOf("深圳")!=-1 || keyword.indexOf("城市公园")!=-1){
				int flag=0;
				if(sdList!=null && sdList.size()>0){
					for (SearchDto searchDto : sdList) {
						if(searchDto.getId().equals("2222")){
							flag=1;
						}
					}
				}
				if(flag==0){
					SearchDto sd2=new SearchDto();
					sd2.setId("2222");
					sd2.setName("辅助决策-深圳城市公园园内实时人数汇总表2019.11.27");
					sd2.setType(0);
					sd2.setDownUrl(filePath+"/work/fzjc2.docx");
					//sd.setImageUrl(filePath+"/work/fzjc.docx");
					sd2.setAddress(null);
					sdList.add(sd2);
				}
			}
			WherePrams wherePrams = Method.where("delFlag", C.EQ, 0);
			wherePrams.and("partName", C.LIKE, keyword);
			List<Parts> result = partsService.list(wherePrams);
			if(result!=null && result.size()>0){
				for (Parts parts : result) {
					SearchDto sd=new SearchDto();
					sd.setId(parts.getId());
					sd.setName(parts.getPartName());
					sd.setType(1);
					sd.setImageUrl(filePath+parts.getFback1());
					sd.setAddress(parts.getPartAddress());
					sdList.add(sd);
				}
			}

			ResultObj obj = new ResultObj(ResultCode.RESULT_SUCCESS, "success", sdList);
			return obj.toString();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			ResultObj robj = new ResultObj(ResultCode.RESULT_EXCEPTION, "网络异常", null);
			return robj.toString();
		}

	}

}
